package com.fafian.gdc.core;

import com.fafian.publish.Listener;
import com.fafian.publish.PublishBus;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;

public class GpioCommandListener implements Listener<BusCommand>{

	private GpioPinDigitalOutput relay1, relay2;
	
	public GpioCommandListener(GpioPinDigitalOutput relay1, GpioPinDigitalOutput relay2){
		
		this.relay1 = relay1;
		this.relay2 = relay2;
	}
	
	public void register(PublishBus bus){
		if (bus!=null){
			bus.registerListener(this, BusCommand.class);
		}
	}

	@Override
	public void notify(BusCommand value) {
		switch(value.getCommand()){
		case PUSH_BUTTON1:
			System.out.println("Switching on relay1");
			if (relay1 != null){
				relay1.pulse(500, PinState.LOW, true);
			}
			break;
		case PUSH_BUTTON2:
			System.out.println("Switching on relay2");
			if (relay2 != null){
				relay2.pulse(500, PinState.LOW, true);
			}
		default:
			break;
	
		}
		
	}
}
